home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / tcoop10a.zip / DEMO.ZIP / TESTITEM.CPP < prev    next >
C/C++ Source or Header  |  1991-11-20  |  2KB  |  90 lines

  1. //
  2. //      TESTITEM.CPP
  3. //      version 1.00    11/10/91
  4. //      uses LIST.CPP for List Class
  5. //      copyright (c) 1991 by James S. Clark
  6. //      all rights reserved
  7. //
  8.  
  9. // #include <string.h>
  10.  
  11. #include "list.hpp"
  12. #include "item.hpp"
  13.  
  14.  
  15. void    printall(List *list)
  16. {
  17.     int    i;
  18.  
  19.     for (i=0; i<list->count(); i++)
  20.         ((Item *) list->get(i))->print();
  21.     puts("");
  22. }
  23.  
  24.  
  25. void    printallback(List *list)
  26. {
  27.     Item    *current = (Item *) list->last;
  28.     int    i;
  29.  
  30.     while (current) {
  31.         current->print();
  32.         current = (Item *) current->previous;
  33.     }
  34.     puts("");
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40.         List    *list = new List(Item::compare);    // create a new list and
  41.                                                     // pass it a compare func
  42.         Item    *test1, *test2;
  43.     int    i;
  44.  
  45.     // this creates two items and add them to the list
  46.     test1 = new Item("Test1 - Item1");
  47.     list->add(test1);
  48.     test2 = new Item("Test1 - Item2");
  49.     list->add(test2);
  50.     test2->print();
  51.     list->destroy();
  52.  
  53.     // this does the job on one line
  54.     test1 = (Item *) list->add(new Item("Test2 - Item 1"));
  55.     test2 = (Item *) list->add(new Item("Test2 - Item 2"));
  56.     test2->print();
  57.     list->destroy();
  58.  
  59.     // you can create a list and 'get' the Items later
  60.     list->add(new Item("Test3 - Item1"));
  61.     list->add(new Item("Test3 - Item2"));
  62.     test1 = (Item *) list->get(0);
  63.     test1->print();
  64.     list->destroy();
  65.  
  66.     // you can create a list using operator overloading
  67.     *list + new Item("Test4 - Item1")
  68.           + new Item("Test4 - Item4")
  69.           + new Item("Test4 - Item2")
  70.           + new Item("Test4 - Item3");
  71.  
  72.     printf("%d is the count\n", list->count());
  73.     printall(list);
  74.  
  75. //
  76. //      the sort method is still a bit buggy.
  77. //      it loses the original pointer...
  78. //      list->sort();
  79. //
  80.  
  81.     test1 = (Item *) list->get(2);
  82.     list->add(test1, new Item("Test4 - *****"));
  83.  
  84.     printall(list);
  85.     printallback(list);
  86.  
  87.     delete list;        // free the list object
  88.     return(0);
  89. }
  90.